<?php

	function sp_result($q_id){
		
		if( empty($q_id) ) return '';
		if( !is_numeric($q_id) ) return '';
		
		$out = '';
		$CI = &get_instance();
		
		// Ищем голосование с таким ID
		$CI->db->select('q_question,q_totalvotes')->limit(1)->where('q_id',$q_id);
		$questions = $CI->db->get('sp_questions');
		
		if( $questions->num_rows() ){
			
			$q = $questions->row();
			
			$CI->load->library('table');
			$tmpl = array ( 'table_open'  => '<table class="sp_results">' );
			$CI->table->set_template($tmpl); 			
			
			$CI->table->add_row('');
			$CI->table->add_row("<strong>{$q->q_question}</strong>");
			
			// Находим все варианты ответов
			$CI->db->select('a_answer,a_votes');
			$CI->db->where('a_qid',$q_id);
			$CI->db->order_by('a_order');
			$answers = $CI->db->get('sp_answers');
			
			if( $answers->num_rows() ){
				
				foreach( $answers->result() as $a ){
					
					$line = getinfo('plugins_url') . 'samborsky_polls/img/1.gif';
					
					$percent = $q->q_totalvotes ? ($a->a_votes/$q->q_totalvotes)*100 : 0.00;
					$percent_width = ceil($percent);
					$percent = round($percent,2);
					
					$CI->table->add_row("{$a->a_answer} - {$a->a_votes} ({$percent}%)");
					$CI->table->add_row("<img src=\"$line\" style=\"border: 1px solid black;\" width=\"{$percent_width}%\" height=\"10\">");
				}
			}
			
			$total = number_format($q->q_totalvotes,0,' ',' ');
			$archive = mso_get_option('sp_archive_url');

			$CI->table->add_row('');
			$CI->table->add_row("<strong>Всего проголосовало:</strong> {$total}");
			$CI->table->add_row('');
			$CI->table->add_row("<a href=\"{$archive}\">Архив</a>");
		}
		
		$out = $CI->table->generate();
		
		return $out;
		
	}

?>